import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.sql.*;
import java.util.Objects;
import java.util.Scanner;

public class test {
    public static void main(String[] args) throws Exception {
        while (true){
            welcome();
        }
    }

    public static void login() throws Exception {
        //1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/login";
        String user = "root";
        String password = "111214Cjj";

        Connection conn = DriverManager.getConnection(url, user, password);
        //接受用户输入用户名和密码
        Scanner sc = new Scanner(System.in);
        System.out.println("如果是管理员登录，请输入管理员的用户名和密码");
        System.out.println("请输入用户名");
        String name = sc.next();
        System.out.println("请输入密码");
        String pwd = sc.next();

        String sql = "select * from user where username = ? and userpwd = ?";
        //获取pstmt对象
        PreparedStatement pstmt = conn.prepareStatement(sql);

        //设置？的值
        pstmt.setString(1, name);
        pstmt.setString(2, pwd);
        //执行sql
        ResultSet rs = pstmt.executeQuery();

        User pop = null;
        if (rs.next()) {
            System.out.println("登录成功");
            pop = new User();
            pop.setName(name);
            pop.setPwd(pwd);

            if((Objects.equals(pop.getName(), "admin")) && (Objects.equals(pop.getPwd(), "admin"))){
                loginadmin(pop);
            }else {
                showUser(pop);
            }
        } else {
            System.out.println("登录失败");
        }




        rs.close();
        pstmt.close();
        conn.close();
    }

    public static void registerUser(User pop) throws Exception {

        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/login";
        String username = "root";
        String password = "111214Cjj";

        try {
            Connection connection = DriverManager.getConnection(url, username, password); // 获取数据库连接
        } catch (Exception e) {
            e.printStackTrace();
        }

        String sql = "INSERT INTO user (userName, userPwd) VALUES (?, ?)";
        String sql2 = "INSERT INTO files (filedri) VALUES (?)";
            Connection connection = DriverManager.getConnection(url, username, password);
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            PreparedStatement preparedStatement2 = connection.prepareStatement(sql2);

            preparedStatement.setString(1, pop.getName());
            preparedStatement.setString(2, pop.getPwd());
            preparedStatement2.setString(1,"D:\\users\\user" + pop.getName());

            preparedStatement.executeUpdate();
            preparedStatement2.executeUpdate();






        File newFolder = new File("D:\\users\\user"+pop.getName());
        boolean isCreated = newFolder.mkdir();

    }

    public static void welcome() throws Exception {
        System.out.println("按1进行用户登录");
        System.out.println("按2进行用户注册");
        char ch;
        Scanner sc = new Scanner(System.in);
        ch = sc.next().charAt(0);
        switch (ch){
            case '1':
                login();//登录
                break;
            case '2'://注册
                System.out.println("开始注册");
                User pop = new User();
                System.out.println("输入名字");
                String userName = sc.next();
                System.out.println("输入密码");
                String userPwd = sc.next();
                pop.setName(userName);
                pop.setPwd(userPwd);
                registerUser(pop);
                System.out.println("注册成功");
            default:
                break;
        }
    }

    public static void showUser(User user) throws Exception {
        while (true) {
            System.out.println("按1查看文件列表");
            System.out.println("按2文件上传");
            System.out.println("按3文件下载");
            System.out.println("按4文件删除");
            System.out.println("按5修改自身信息");
            System.out.println("按6退出登录");

            Scanner sc = new Scanner(System.in);
            char op = sc.next().charAt(0);
            switch (op) {
                case '1':
                    showFile(user);
                    break;
                case '2':
                    File(user);
                    break;
                case '3':
                    downLoad(user);
                    break;
                case '4':
                    deleted(user);
                    break;
                case '5':
                    modify(user);
                    break;
                case '6':
                    System.out.println("您已经退出登录");
                    return;
            }
        }
    }

    public static void File(User user) throws Exception {

        //文件上传
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/login";
        String username = "root";
        String password = "111214Cjj";

        Connection conn = DriverManager.getConnection(url, username, password);

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入文件地址");
        String file1 = sc.nextLine();
        File src = new File(file1);


        user.setFile(src);
        String nowname = user.getName();
        String sql = "SELECT userID FROM user WHERE username = ?";
        //获取pstmt对象
        PreparedStatement pstmt = conn.prepareStatement(sql);

        //设置？的值
        pstmt.setString(1, nowname);
        //执行sql
        ResultSet rs = pstmt.executeQuery();
        int id = 0;
        if (rs.next()) {
            id = rs.getInt("userID");
        }
        String fileadddress = "D:\\users\\user" + user.getName();
        String sql2 = "update files set filedri = ? where id = ?";
        PreparedStatement pstmt2 = conn.prepareStatement(sql2);
        pstmt2.setString(1, fileadddress);
        pstmt2.setInt(2, id);
        int rs2 = pstmt2.executeUpdate();

        File old = new File(file1);
        File xin = new File(fileadddress);

        String fileName = new File(file1).getName();
        // 新文件的名称
        //String fileName = user.getName()+".txt";
        // 完整的文件路径
        String filePath = fileadddress + File.separator + fileName;

        // 使用 try-with-resources 来自动关闭资源
        try (FileWriter fw = new FileWriter(filePath);
             FileOutputStream fos = new FileOutputStream(filePath)) {



        } catch (IOException e) {
            e.printStackTrace();
        }
        copydir(file1,fileadddress,filePath);

        System.out.println("成功上传文件");

        rs.close();
        pstmt.close();
        conn.close();
    }

    private static void copydir(String src, String dest,String filePath) throws Exception {

        try (FileReader reader = new FileReader(src);
             FileWriter writer = new FileWriter(filePath)) {

            int charRead;
            // 循环读取字符并写入到目标文件中
            while ((charRead = reader.read()) != -1) {
                writer.write(charRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
            // 处理异常情况
        }
    }

    public static void showFile(User user) throws Exception {
        //查看文件
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/login";
        String username = "root";
        String password = "111214Cjj";

        Connection conn = DriverManager.getConnection(url, username, password);

        String nowname = user.getName();
        String sql = "SELECT userID FROM user WHERE username = ?";
        PreparedStatement pstmt = conn.prepareStatement(sql);

        //设置？的值
        pstmt.setString(1, nowname);
        //执行sql
        ResultSet rs = pstmt.executeQuery();
        int id = 0;
        if (rs.next()) {
            id = rs.getInt("userID");
        }


        String sql2 = "select * from files where id = ?";
        PreparedStatement pstmt2 = conn.prepareStatement(sql2);

        pstmt2.setInt(1, id);
        ResultSet rs2 = pstmt2.executeQuery();
        String columnValue = null;
        while (rs2.next()) {
            columnValue = rs2.getString("filedri");
            //System.out.println(columnValue);
        }

        File directory = new File(columnValue);
        // 确保该目录存在
        if (directory.exists() && directory.isDirectory()) {
            // 获取并遍历该目录中的文件
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    // 打印文件名称
                    System.out.println(file.getName());
                }
            }
        } else {
            System.out.println(columnValue + " is not a valid directory.");
        }

        rs.close();
        pstmt.close();
        conn.close();
    }

    public static void downLoad(User user) throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要下载的文件名称");
        String documentpath = sc.next();
        // 文件URL
        String fileUrl = "file:/D:/users/user"+user.getName()+"/"+documentpath;
        // 文件保存路径
        System.out.println("请输入您的文件保存路径");
        String savePath = sc.next();
        //String savePath = "D:\\download";
        // 文件名
        String fileName = user.getName()+"file";

        // 创建URL对象
        URL url = new URL(fileUrl);
        // 创建URLConnection对象
        URLConnection connection = url.openConnection();
        // 创建输入流
        InputStream in = connection.getInputStream();
        // 创建输出流
        FileOutputStream out = new FileOutputStream(savePath + fileName);


        // 读取并写入文件
        byte[] buffer = new byte[1024];
        int len;
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }

        // 关闭流
        in.close();
        out.close();

        System.out.println("File downloaded successfully.");
    }


    public static void deleted(User user){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的文件名称");
        String filename = sc.next();
        // 文件路径
        //String filePath = "D:\\users\\user张三\\张三.txt";
        String filePath = "D:\\users\\user"+user.getName()+"\\"+filename;
        // 删除文件
        boolean deleted = new File(filePath).delete();
        System.out.println("File deleted: " + deleted);
    }

    public static void modify(User user) throws Exception {
        System.out.println("如果要修改密码请按1");
        System.out.println("如果要修改用户名请按2");
        System.out.println("如果是不小心按到的请按3退出");
        char ch;
        Scanner sc = new Scanner(System.in);
        ch = sc.next().charAt(0);
        switch (ch){
            case '1':
                System.out.println("请输入初始密码");
                String originpassword = sc.next();
                if(Objects.equals(originpassword, user.getPwd())){
                    System.out.println("请输入新密码");
                    String newpassword = sc.next();

                    user.setPwd(newpassword);
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    //2.获取连接
                    String url = "jdbc:mysql://127.0.0.1:3306/login";
                    String username = "root";
                    String password = "111214Cjj";

                    try {
                        Connection connection = DriverManager.getConnection(url, username, password); // 获取数据库连接
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    String sql = "update user set userPwd = ? where userName = ?";
                    Connection connection = DriverManager.getConnection(url, username, password);
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, newpassword);
                    preparedStatement.setString(2, user.getName());
                    preparedStatement.executeUpdate();
                    System.out.println("恭喜你，密码修改成功");

                    break;
                }
                else {
                    System.out.println("密码输入错误");
                    break;
                }
            case '2':
                System.out.println("请输入您的新用户名");
                String newname = sc.next();
                Class.forName("com.mysql.cj.jdbc.Driver");
                //2.获取连接
                String url = "jdbc:mysql://127.0.0.1:3306/login";
                String username = "root";
                String password = "111214Cjj";

                String sql5 = "SELECT userID FROM user WHERE username = ?";
                Connection conn5 = DriverManager.getConnection(url, username, password);
                //获取pstmt对象
                PreparedStatement pstmt = conn5.prepareStatement(sql5);

                //设置？的值
                pstmt.setString(1, user.getName());
                //执行sql
                ResultSet rs = pstmt.executeQuery();
                int id = 0;
                if (rs.next()) {
                    id = rs.getInt("userID");
                }
                try {
                    Connection connection = DriverManager.getConnection(url, username, password); // 获取数据库连接
                } catch (Exception e) {
                    e.printStackTrace();
                }

                String sql = "update user set userName = ? where userName = ?";
                String sql2 = "update files set filedri = ? where id = ?";
                Connection connection = DriverManager.getConnection(url, username, password);
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                PreparedStatement preparedStatement2 = connection.prepareStatement(sql2);
                preparedStatement.setString(1, newname);
                preparedStatement.setString(2, user.getName());
                preparedStatement2.setString(1,"D:\\users\\user" + newname );
                preparedStatement2.setInt(2, id);
                preparedStatement.executeUpdate();
                preparedStatement2.executeUpdate();

                System.out.println("修改用户名成功");
                String oldFolderPath = "D:\\users\\user"+user.getName();
                // 新文件夹名称
                String newFolderName = "D:\\users\\user"+newname;
                // 修改文件夹名称
                File folder = new File(oldFolderPath);
                boolean b = folder.renameTo(new File(newFolderName));
                user.setName(newname);
                break;

            case '3':
                return;
        }
    }


    public static void loginadmin(User user) throws Exception {

        while (true) {
            System.out.println("您已经成功进入管理员界面");
            System.out.println("按1查看用户");
            System.out.println("按2修改用户");
            System.out.println("按3删除用户");
            System.out.println("按4退出登录");
            Scanner sc = new Scanner(System.in);
            char ch;
            ch = sc.next().charAt(0);
            switch (ch) {
                case '1':
                    adminshow(user);
                    break;
                case '2':
                    adminmodify(user);
                    break;
                case '3':
                    admindelete();
                    break;
                case '4':
                    return;
            }
        }
    }

    public static void adminshow(User user) throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/login";
        String username = "root";
        String password = "111214Cjj";

        Connection conn = DriverManager.getConnection(url, username, password);
        String sql = "select * from user";
        String sql2 = "select * from files";


        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        PreparedStatement preparedStatement2 = conn.prepareStatement(sql2);
        // 执行查询
        ResultSet resultSet = preparedStatement.executeQuery();
        ResultSet resultSet2 = preparedStatement2.executeQuery();
        // 遍历结果集
        while(resultSet.next() && resultSet2.next()) {
            // 获取每一行数据的具体字段值
            String field1 = resultSet.getString("userName");
            String field2 = resultSet.getString("userPwd");
            String field3 = resultSet2.getString("filedri");

            System.out.print("用户名"+"    "+field1);
            System.out.print("    ");
            System.out.print("密码"+"    "+field2);
            System.out.println("    ");
            System.out.print("文件地址"+"    "+field3);
            System.out.println("    ");
        }
        // 关闭资源
        resultSet2.close();
        resultSet.close();
        preparedStatement.close();
        conn.close();
    }

    public static void adminmodify(User user) throws Exception {
        System.out.println("请输入你要修改的用户名字");
        Scanner sc = new Scanner(System.in);
        String name = sc.next();

        //文件上传
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/login";
        String username = "root";
        String password = "111214Cjj";

        Connection conn = DriverManager.getConnection(url, username, password);
        while (true) {
            System.out.println("按1修改用户的密码");
            System.out.println("按2修改用户的用户名");
            System.out.println("按3退出");
            char ch;
            ch = sc.next().charAt(0);
            switch (ch) {
                case '1':
                    System.out.println("请输入新密码");
                    String newpassword = sc.next();

                    String sql = "update user set userPwd = ? where userName = ?";
                    PreparedStatement preparedStatement = conn.prepareStatement(sql);
                    preparedStatement.setString(1, newpassword);
                    preparedStatement.setString(2, name);
                    preparedStatement.executeUpdate();
                    System.out.println("恭喜你，密码修改成功");

                    break;
                case '2':
                    System.out.println("请输入您的新用户名");
                    String newname = sc.next();

                    String sql5 = "SELECT userID FROM user WHERE username = ?";
                    Connection conn5 = DriverManager.getConnection(url, username, password);
                    //获取pstmt对象
                    PreparedStatement pstmt = conn5.prepareStatement(sql5);

                    //设置？的值
                    pstmt.setString(1, name);
                    //执行sql
                    ResultSet rs = pstmt.executeQuery();
                    int id = 0;
                    if (rs.next()) {
                        id = rs.getInt("userID");
                    }

                    String sql2 = "update user set userName = ? where userName = ?";
                    String sql4 = "update files set filedri = ? where id = ?";
                    Connection connection = DriverManager.getConnection(url, username, password);
                    Connection conn4 = DriverManager.getConnection(url, username, password);
                    PreparedStatement preparedStatement2 = connection.prepareStatement(sql2);
                    PreparedStatement preparedStatement4 = conn4.prepareStatement(sql4);
                    preparedStatement2.setString(1, newname);
                    preparedStatement2.setString(2, name);
                    preparedStatement4.setString(1,"D:\\users\\user" + newname );
                    preparedStatement4.setInt(2, id);
                    preparedStatement2.executeUpdate();
                    preparedStatement4.executeUpdate();

                    System.out.println("修改用户名成功");
                    String oldFolderPath = "D:\\users\\user"+name;
                    // 新文件夹名称
                    String newFolderName = "D:\\users\\user"+newname;
                    // 修改文件夹名称
                    File folder = new File(oldFolderPath);
                    boolean b = folder.renameTo(new File(newFolderName));
                    user.setName(newname);
                    break;
                case '3':
                    return;
                default:
                    break;
            }
        }
    }
    public static void admindelete() throws Exception {

        System.out.println("请输入要删除的用户名称");
        Scanner sc = new Scanner(System.in);
        String name = sc.next();
        //文件上传
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/login";
        String username = "root";
        String password = "111214Cjj";

        Connection conn = DriverManager.getConnection(url, username, password);
        Connection conn2 = DriverManager.getConnection(url, username, password);
        String sql5 = "SELECT userID FROM user WHERE username = ?";
        Connection conn5 = DriverManager.getConnection(url, username, password);
        //获取pstmt对象
        PreparedStatement pstmt = conn5.prepareStatement(sql5);

        //设置？的值
        pstmt.setString(1, name);
        //执行sql
        ResultSet rs = pstmt.executeQuery();
        int id = 0;
        if (rs.next()) {
            id = rs.getInt("userID");
        }


        String sql = "delete from files where id = ?";
        String sql2 = "delete from user where userID = ?";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        PreparedStatement preparedStatement2 = conn2.prepareStatement(sql2);
        preparedStatement.setInt(1, id);
        preparedStatement2.setInt(1, id);

        preparedStatement.executeUpdate();
        preparedStatement2.executeUpdate();
        System.out.println("删除用户成功");


        // 文件路径
        //String filePath = "D:\\users\\user张三\\张三.txt";
        String filePath = "D:\\users\\user"+name+"\\";
        // 删除文件
        boolean deleted = new File(filePath).delete();
        System.out.println("File deleted: " + deleted);
    }


}






























